package cn.kgc.springcloud.nacos.controller;

import com.alibaba.cloud.nacos.discovery.NacosDiscoveryClientConfiguration;
import com.alibaba.cloud.nacos.ribbon.NacosRibbonClientConfiguration;
import com.alibaba.cloud.nacos.ribbon.NacosRule;
import com.google.errorprone.annotations.Var;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author: mosin
 * @version: v1.0  2022/8/17
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private RestTemplate  restTemplate;



    @RequestMapping("test1")
    public String  test1(){
       return "user controller ok" ;
    }

    @RequestMapping("test2")
    public String  test2(){
        // 调用角色服务  RoleController    http://localhost:8851/role/test1  使用java发送 http请求
        // 创建RestTemplate实例  发送http请求
        RestTemplate restTemplate = new RestTemplate();
        String info = restTemplate.getForObject("http://"+getHost()+"/role/test1", String.class);
        System.out.println("info = " + info);
        return "user controller ok, info =" + info;
    }

    //  1. 没有健康检查服务  2.负载均衡的策略单一
    public String getHost(){
        ArrayList<String> hosts = new ArrayList<>();
        hosts.add("localhost:8851");
        hosts.add("localhost:8852");
        hosts.add("localhost:8853");
        return hosts.get(new Random().nextInt(hosts.size()));
    }


    @RequestMapping("test3")
    public String  test3(){
        // 调用角色服务  RoleController    http://localhost:8851/role/test1  使用java发送 http请求
        // 创建RestTemplate实例  发送http请求
        RestTemplate restTemplate = new RestTemplate();

        // 获取请求的uri
        List<ServiceInstance> roleclient = discoveryClient.getInstances("ROLECLIENT");
        ServiceInstance serviceInstance = roleclient.get(new Random().nextInt(roleclient.size()));
        URI uri = serviceInstance.getUri();
        System.out.println("uri = " + uri);  //http://192.168.8.152:8852
        String info = restTemplate.getForObject(uri+"/role/test1", String.class);
        System.out.println("info = " + info);
        return "user controller ok, info ="+info;
    }


    @RequestMapping("test4")
    public String  test4(){
        // 调用角色服务  RoleController    http://localhost:8851/role/test1  使用java发送 http请求
        // 创建RestTemplate实例  发送http请求
        RestTemplate restTemplate = new RestTemplate();
        // 获取请求的uri choose() 返回一个可用实例
        ServiceInstance serviceInstance = loadBalancerClient.choose("ROLECLIENT");
        URI uri = serviceInstance.getUri();
        System.out.println("uri = " + uri);  //http://192.168.8.152:8852
        String info = restTemplate.getForObject(uri+"/role/test1", String.class);
        System.out.println("info = " + info);
        return "user controller ok, info ="+info;
    }


    @RequestMapping("test5")
    public String  test5(){

        //  从哪spring容器中获取的restTemplate      192.168.8.152:8852
        String info = restTemplate.getForObject("http://ROLECLIENT/role/test1", String.class);
        return "user controller ok, info ="+info;
    }

}
